<!-- 客户信息管理 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="客户名称">
                            <a-input v-model="customerName" placeholder="请输入客户名称" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="统一社会信用代码">
                            <a-input v-model="socialCode" placeholder="请输入统一社会信用代码" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="手机号">
                            <a-input v-model="contactPhone" placeholder="请输入手机号" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="姓名">
                            <a-input v-model="contactName" placeholder="请输入姓名" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="来源">
                            <a-select v-model="origin" placeholder="请选择">
                                <a-select-option v-for="item in sourceList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="结算单需要审核">
                            <a-select v-model="settlementNeedExamine" placeholder="请选择">
                                <a-select-option v-for="item in hasAuditList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='16' :sm='24'>
                        <a-form-item label="创建时间">
                            <a-range-picker :show-time="{ format: 'HH:mm:ss' }" format="YYYY-MM-DD HH:mm:ss" @ok="onDateChange" allow-clear/>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button type='primary' @click="getData">查询</a-button>
                            <a-button style="margin-left: 20px" type='' @click="add">添加客户</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ total: total ,showSizeChanger: true,pageSize: size,}" size="small" :scroll="{ x: 1500, }">
            <div slot="origin" slot-scope="origin">
                <div v-for="(item,i) in sourceList" :key="item.id">
                    <a-tag v-if="item.id == origin" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="settlementNeedExamine" slot-scope="settlementNeedExamine">
                <div v-for="(item,i) in hasAuditList" :key="item.id">
                    <a-tag v-if="item.id == settlementNeedExamine" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="createdBy" slot-scope="createdBy,record">
                <div>{{record.updatedBy ?? createdBy}}</div>
            </div>
            <div slot="operation" slot-scope="text,record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="detail(record)">详情</a-button>
                    <a-popconfirm
                        title="是否删除当前客户信息?"
                        ok-text="是"
                        cancel-text="否"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </div>
        </a-table>

        <a-modal 
            v-model="visible" 
            title="添加客户"
            @ok="handleOk" 
            :forceRender="true"
            @cancel="visible = false">
            <div>
                <a-tabs v-model="active" size="small">
                    <a-tab-pane key="1" tab="创建新的客户">
                        <a-form-model :model="info" ref="ruleForm" :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
                            <a-form-model-item label="客户名称" prop="customerName">
                                <a-input v-model="info.customerName" :maxLength="40"  allow-clear placeholder="请输入客户名称" />
                            </a-form-model-item>
                            <a-form-model-item label="统一社会信用代码" prop="socialCode">
                                <a-input v-model="info.socialCode" allow-clear placeholder="请输入统一社会信用代码" />
                            </a-form-model-item>
                            <a-form-model-item label="姓名" prop="contactName">
                                <a-input v-model="info.contactName" allow-clear placeholder="请输入姓名" />
                            </a-form-model-item>
                            <a-form-model-item label="手机号" prop="contactPhone">
                                <a-input v-model="info.contactPhone" allow-clear placeholder="请输入手机号" />
                            </a-form-model-item>
                            <a-form-model-item label="初始密码" help="未填写默认88888888，6-20位，支持数字，字母，区分大小写">
                                <a-input v-model="info.password" allow-clear placeholder="请输入初始密码" />
                            </a-form-model-item>
                        </a-form-model>
                    </a-tab-pane>
                    <a-tab-pane key="2" tab="关联平台已存在的客户" force-render>
                        <a-form-model :form="form" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
                            <a-form-model-item label="客户名称" >
                                <a-select v-model="relation.customerId" placeholder="请选择" @change="selRelaCustomer">
                                    <a-select-option v-for="item in customerList" :key="item.id" :value="item.id">{{item.customerName}}</a-select-option>
                                </a-select>
                            </a-form-model-item>
                            <a-form-model-item label="统一社会信用代码">
                                <a-input v-model="relation.socialCode" :disabled="true" v-decorator="['socialCode', { rules: [{required: true, message: '请输入统一社会信用代码'}] }]" allow-clear placeholder="请输入统一社会信用代码" />
                            </a-form-model-item>
                        </a-form-model>
                    </a-tab-pane>
                </a-tabs>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { createCustomer , queryCustomerList , queryAllCustomerList , queryUserCustomerList , relationCustomer , deleteCustomer } from '@/api/services/customer';
export default {
    data () {
        return {
            isload: false,
            dataList: [],
            customerName: '', // 客户名称
            socialCode: '', // 统一社会信用代码
            contactName: '', // 姓名
            contactPhone: '', // 联系手机号
            origin: undefined, // 来源
            settlementNeedExamine: undefined, // 结算是否需要审核
            page: 1,
            size: 10,
            total: 0,
            startTime: '',
            endTime: '',
            sourceList: [
                { id: 1, name: '创建'},
                { id: 2, name: '平台关联'}
            ],
            hasAuditList: [
                { id: 0, name: '不需要'},
                { id: 1, name: '需要'}
            ],
            columns,
            form: this.$form.createForm(this.info),
            info: {
                customerName: '', // 客户名称
                socialCode: '', // 社会信用代码
                contactName: '', // 姓名
                contactPhone: '', // 手机号
                password: '', // 初始密码
            },
            visible: false,
            active: '1',
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            customerList: [], // 平台关联客户列表
            rules: {
                customerName: [
                    { required: true, message: '请输入客户名称', trigger: 'blur' },
                    { max: 40, message: '客户名称最长不能超过40', trigger: 'blur' },
                ],
                socialCode: [{ required: true, message: '请输入统一社会信用代码', trigger: 'blur' }],
                contactName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                contactPhone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { pattern: /^1[3456789]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'}
                ],
            },
            relation: {
                customerId: undefined,
                socialCode: '',
            }
            
        };
    },
    created(){
        this.getData();
    },
    methods: {
        // 日期筛选
        onDateChange(date,dateStr){
            console.log(date,dateStr)
            if(date.length > 0){
                this.startTime = date[0].format('YYYY-MM-DD HH:mm:ss')
                this.endTime = date[1].format('YYYY-MM-DD HH:mm:ss')
            }else{
                this.startTime = ''
                this.endTime = ''
            }
            this.getData()
        },
        // 获取账户列表数据
        getData(){
            let _this = this
            _this.isload = true;
            const params = {
                'startTime': _this.startTime,
                'endTime': _this.endTime,
                'customerName': _this.customerName,
                'socialCode': _this.socialCode,
                'contactName': _this.contactName,
                'contactPhone': _this.contactPhone,
                'origin': _this.origin,
                'settlementNeedExamine': _this.settlementNeedExamine,
                'pageSize': _this.size,
                'pageIndex': _this.page,
            }
            queryCustomerList(params)
            .then(res=>{
                _this.isload = false;
                if(res.success){
                    _this.dataList = res.data;
                    _this.total = res.totalCount;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加客户
        add(){
            // this.$refs.ruleForm.resetFields();
            this.info.id = ''
            this.info.customerName = ''
            this.info.socialCode = ''
            this.info.contactName = ''
            this.info.contactPhone = ''
            this.info.password = ''
            this.relation.customerId = undefined
            this.getOriginSystemCustomerList();
            this.visible = true
        },
        handleOk(){
            let _this = this
            console.log(_this.active,'active')
            if(_this.active == 1){
                if(_this.info.customerName == ''){
                    _this.$message.warning('请输入客户名称')
                    return;
                }
                if(_this.info.socialCode == ''){
                    _this.$message.warning('请输入统一社会信用代码')
                    return;
                }
                if(_this.info.contactName == ''){
                    _this.$message.warning('请输入客户姓名')
                    return;
                }
                if(_this.info.contactPhone == '' || _this.info.contactPhone.length != 11){
                    _this.$message.warning('请输入正确的客户手机号')
                    return;
                }
                const params = {
                    'customerName': _this.info.customerName, // 客户名称
                    'socialCode': _this.info.socialCode, // 统一社会信用代码
                    'contactName': _this.info.contactName, // 客户姓名
                    'contactPhone': _this.info.contactPhone, // 客户手机号
                    'password': _this.info.password, // 初始密码
                }
                createCustomer(_this.info)
                .then(res=>{
                    if(res.success){
                        _this.$notification['success']({
                            message: '提示',
                            description: '添加成功',
                            duration: 8,
                        })
                        _this.visible = false;
                        _this.getData();
                    }else{
                        _this.$notification['error']({
                            message: '提示',
                            description: res.errMessage,
                            duration: 8,
                        })
                    }
                })
            }else{
                if(!_this.relation.customerId){
                    _this.$message.warning('请选择平台客户信息')
                    return;
                }
                relationCustomer(_this.relation.customerId)
                .then(res => {
                    if(res.success){
                        _this.$notification['success']({
                            message: '提示',
                            description: '添加成功',
                            duration: 8,
                        })
                        _this.visible = false;
                        _this.getData();
                    }else{
                        _this.$notification['error']({
                            message: '提示',
                            description: res.errMessage,
                            duration: 8,
                        })
                    }
                })
            }
            
        },
        // 获取平台关联客户列表
        getOriginSystemCustomerList(){
            let _this = this
            queryUserCustomerList()
            .then(res=>{
                if(res.success){
                    _this.customerList = res.data;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 选择平台客户
        selRelaCustomer(id){
            this.relation = this.customerList.find(item => item.id == id);
            this.relation.customerId = this.relation.id
        },
        // 客户详情
        detail(item){
            this.$router.push({path: '/servicesCustomer/customerInfoDetail', query: {'companyId': item.id}})
        },
        // 删除客户
        del(item){
            let _this = this;
            deleteCustomer(item.id)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                    _this.getData();
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>